package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.Customer;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 顾客
 */
@Component
public class CustomerDao implements BaseDao<Customer> {

    @Override
    public int insert(Customer entity) {
        return DBHelper.executeUpdate("insert into customer(realname,idnumber,phone,sex) values(?,?,?,?)",
            entity.getRealname(), entity.getIdnumber(), entity.getPhone(), entity.getSex());
    }

    @Override
    public int update(Customer entity) {
        if (entity.getBirthday() == "" && "".equals(entity.getBirthday())) {
            String sql = "update customer set  realname =?, idnumber=?,phone=?,sex=? where id=?";
            return DBHelper.executeUpdate(sql, entity.getRealname(), entity.getIdnumber(), entity.getPhone(),
                entity.getSex(), entity.getId());
        }
        String sql = "update customer set  realname =?, idnumber=?,phone=?,sex=?,birthday = ? where id=?";
        return DBHelper.executeUpdate(sql, entity.getRealname(), entity.getIdnumber(), entity.getPhone(),
            entity.getSex(), entity.getBirthday(), entity.getId());

    }

    @Override
    public int delete(Customer entity) {
        String sql = "update customer set is_deleted = 1 where id=? ";
        return DBHelper.executeUpdate(sql, entity.getId());
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return DBHelper.executeQuery("select * from customer where is_deleted = 0");
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        Customer customer = (Customer)obj[0];
        String sql = "SELECT c.*,r.name from customer c left join room r on c.room_id=r.id "
            + " where 1=1 and c.is_deleted = 0 ";
        if (customer.getRealname() != "" && !"".equals(customer.getRealname())) {
            sql += " and realname like '%" + customer.getRealname() + "%'";
        }
        if (customer.getPhone() != "" && !"".equals(customer.getPhone())) {
            sql += " and phone like '%" + customer.getPhone() + "%'";
        }
        if (customer.getIdnumber() != "" && !"".equals(customer.getIdnumber())) {
            sql += " and idnumber like '%" + customer.getIdnumber() + "%'";
        }
        return DBHelper.executeQuery(sql + " group by idnumber order by id desc limit ?,? ", page.getBegin(),
            page.getPageSize());
    }

    @Override
    public Map<String, Object> findOne(Customer entity) {
        String sql = "select * from customer where id = ? and is_deleted =0 ";
        return DBHelper.executeQuery(sql, entity.getId()).get(0);
    }

    @Override
    public int count(Object... obj) {
        Customer customer = (Customer)obj[0];
        String sql = " SELECT count(DISTINCT idnumber) counts from customer c left join room r on c.room_id=r.id \n"
            + "left join room_status rs on rs.customer_id=c.id where 1=1 and c.is_deleted = 0 ";
        if (customer.getRealname() != "" && !"".equals(customer.getRealname())) {
            sql += " and realname like '%" + customer.getRealname() + "%'";
        }
        if (customer.getPhone() != "" && !"".equals(customer.getPhone())) {
            sql += " and phone like '%" + customer.getPhone() + "%'";
        }
        if (customer.getIdnumber() != "" && !"".equals(customer.getIdnumber())) {
            sql += " and idnumber like '%" + customer.getIdnumber() + "%'";
        }
        return Integer.valueOf(DBHelper.executeQuery(sql).get(0).get("counts").toString());
    }

    public int finduser(Customer entity) {
        return Integer
            .valueOf(DBHelper.executeQuery("select count(*) counts from customer where phone=? and password=? ",
                entity.getUphone(), entity.getPassword()).get(0).get("counts").toString());
    }

    // /**
    // * @Author: 陈世纪
    // * @Date: 17:57 2020/3/6
    // * @Description: 入住时，添加顾客信息，预防顾客信息重复添加，通过身份证号判断是否有该顾客信息
    // */
    // public List<Map<String,Object>> findIdNumber(String id)
    // {
    // String sql = "select * from customer where idnumber = ?";
    // return DBHelper.executeQuery(sql,id);
    // }
    public Map<String, Object> findOnePhone(Customer customer) {
        String sql = "select * from customer where is_deleted = 0 and phone = ?";
        List<Map<String, Object>> list = DBHelper.executeQuery(sql, customer.getPhone());
        return list.size() > 0 ? list.get(0) : null;
    }

    public int addBasic(Customer customer, String roomId) {
        String sql = "insert into customer(realname,idnumber,phone,sex,room_id) values(?,?,?,?,?)";
        return DBHelper.executeUpdate(sql, customer.getRealname(), customer.getIdnumber(), customer.getPhone(),
            customer.getSex(), roomId);
    }

}
